#include "welcomedialog.h"
#include "ui_welcomedialog.h"



/*!
    \fn WelcomeDialog
    Constructor of the WelcomeDialog
  */
WelcomeDialog::WelcomeDialog(SystemController *controller, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::WelcomeDialog)
{
    //Init the UI
    ui->setupUi(this);
    ui->listWidget->setSelectionMode(QListWidget::SingleSelection);

    //Set the Variables
    this->systemController = controller;
    this->savePath = "";
    this->newFile = false;

    //Connect the Slots
    connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(okButton_triggered()));

    //Fill the ListView
    QStringList list = this->systemController->getRecentlyUsedDbs();
    for (int i = 0; i < list.size(); ++i)
    {
        if (QFile::exists(list.at(i)))
            ui->listWidget->addItem(list.at(i));
    }

    if (ui->listWidget->count() > 0)
        ui->listWidget->item(0)->setSelected(true);
}


/*!
    \fn ~WelcomeDialog
    Desstructor of the WelcomeDialog
  */
WelcomeDialog::~WelcomeDialog()
{
    delete ui;
}

////////////////////////////////////////
//Functions for actions in the dialog//
//////////////////////////////////////

/*!
  \fn on_pushButton_clicked
  Called when open existing File clicked
  */
void WelcomeDialog::on_pushButton_clicked()
{
    //Get the filename
    QString fileName = QFileDialog::getOpenFileName(this, tr("Chose database file"), QDir::homePath(),
             tr("TrainerManagementDatabaseFiles (*.tmdb)"));
    this->savePath = QDir::toNativeSeparators(fileName);

    //Writes the selected Filename to the Textbox
    ui->lineEdit->setText(this->savePath);

    //Remove the selection from the Listbox
    ui->listWidget->selectionModel()->clearSelection();

    this->newFile = false;
}



/*!
  \fn on_pushButton_clicked
  Called when create new File clicked
  */
void WelcomeDialog::on_pushButton_2_clicked()
{
    //Get the filename
    QString fileName = QFileDialog::getSaveFileName(this, tr("Chose savepath"), QDir::homePath(),
             tr("TrainerManagementDatabaseFiles (*.tmdb)"));
    this->savePath = QDir::toNativeSeparators(fileName);

    //Writes the selected Filename to the Textbox
    ui->lineEdit->setText(this->savePath);

    //Remove the selection from the Listbox
    ui->listWidget->selectionModel()->clearSelection();

    this->newFile = true;
}

/*!
  \fn on_listWidget_itemSelectionChanged
  Called when an other item in the listWidget is selected
  */
void WelcomeDialog::on_listWidget_itemSelectionChanged()
{
    if (ui->listWidget->selectedItems().size() > 0)
    {
        this->savePath = ui->listWidget->selectedItems().value(0)->text();
        ui->lineEdit->clear();
        this->newFile = false;
    }
}





////////////////////////////////////////////
//Functions for starting the main Program//
//////////////////////////////////////////

/*!
  \fn okButton_triggered
  Called when ok Button clicked
  */
void WelcomeDialog::okButton_triggered()
{
    if(this->savePath != "")
    {
        //Check if it needed to copy a new databaseFile
        if (this->newFile)
        {
            QString sourcePath = QDir::toNativeSeparators(QDir::currentPath());
            sourcePath.append(QDir::separator()).append("db.tmdb");

            //Delete the old existing File
            QFile::remove(this->savePath);

            //Copy the File
            bool success;
            success = QFile::copy(sourcePath, this->savePath);
            if (!success)
            {
                this->systemController->showErrorDialog(this, tr("Error while writing"), tr("While writing the File an error occoured"));
                return;
            }
        }

        //Check the consistency of the File
        this->systemController->setDatabsePath(this->savePath);
        QString errorMsg = this->systemController->checkDatabase();
        if (errorMsg!= "")
        {
            this->systemController->showErrorDialog(this, tr("Defect database"), tr("The chosen database is defect"), errorMsg);
            return;
        }

        //Add the Path to the QSettings
        this->systemController->addRecentlyUsedDbs(this->savePath);

        //Close the Dialog and go the Main Window
        this->setResult(QDialog::Accepted);
        this->hide();

    }
    else
        this->systemController->showErrorDialog(this, tr("No database chosent"), tr("You haven't chose any database file"));
}
